﻿package src.plugin.buildin 
{
  import flash.display.Shape;
  import flash.display.SimpleButton;
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.events.NetStatusEvent;
  import flash.events.ProgressEvent;
	import flash.media.SoundTransform;
  import flash.media.Video;
  import flash.net.NetConnection;
  import flash.net.NetStream;
  import flash.utils.clearInterval;
  import flash.utils.setInterval;
  
  /**
   * 播放器控制面板
   * @author  MeatHill
   * @version 0.1(2009-09-30)
   */
  public class playerControlPanelView extends Sprite
  {
    private var _play_btn:SimpleButton;
    private var _pause_btn:SimpleButton;
    private var _stop_btn:SimpleButton;
		private var _mute_btn:SimpleButton;
		private var _silent_btn:SimpleButton;
    private var _loaded_bar:Sprite;
    private var _played_bar:Shape;
		private var _sound_bg:Sprite;
		private var _sound_bar:Shape;
    private var _ns:NetStream;
    private var _nc:NetConnection;
    private var _video:Video;
    private var _movie_src:String;
		private var _volume:SoundTransform;
    private var _width_uint:uint = 180;
    private var _height_uint:uint = 135;
    private var _is_playing_bl:Boolean = false;
    private var _is_inside_bl:Boolean = false;
    private var _movie_length_uint:uint = 1;
    private var _ori_width_uint:uint = 180;
    private var _ori_height_uint:uint = 135;
    private var _load_interval:uint = 0;
    private var _play_interval:uint = 0;
    
    public function playerControlPanelView(src:String, w:uint = 180, h:uint = 135) 
    {
      _movie_src = src;
      _width_uint = w;
      _height_uint = h;
      
      _play_btn = SimpleButton(getChildAt(2));
      _play_btn.addEventListener(MouseEvent.CLICK, playHandler);
      
      _pause_btn = SimpleButton(getChildAt(0));
      _pause_btn.addEventListener(MouseEvent.CLICK, pauseHandler);
      
      _stop_btn = SimpleButton(getChildAt(1));
      _stop_btn.addEventListener(MouseEvent.CLICK, stop_handler);
      
      _played_bar = Shape(getChildAt(4));
      _loaded_bar = Sprite(getChildAt(3));
			_loaded_bar.buttonMode = true;
      _loaded_bar.addEventListener(MouseEvent.CLICK, setPlayheadHandler);
			
			_silent_btn = SimpleButton(getChildAt(5));
			_silent_btn.addEventListener(MouseEvent.CLICK, silentHandler);
			
			_mute_btn = SimpleButton(getChildAt(6));
			_mute_btn.addEventListener(MouseEvent.CLICK, muteHandler);
			
			_sound_bar = Shape(getChildAt(8));
			_sound_bg = Sprite(getChildAt(7));
			_sound_bg.buttonMode = true;
			_sound_bg.addEventListener(MouseEvent.CLICK, setVolume);
      
      _nc = new NetConnection();
      _nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
      _nc.connect(null);
			
			_volume = new SoundTransform();
      
      addEventListener(Event.REMOVED_FROM_STAGE, removedHandler);
    }
    
    /***************
     * functions
     * ************/
    private function playHandler(evt:MouseEvent = null):void { 
      if (_is_playing_bl) {
        _ns.resume();
      } else {
        _ns.play(_movie_src);
      }
      _play_btn.visible = false;
      _pause_btn.visible = true;
      _play_interval = setInterval(playingHandler, 200);
      _load_interval = setInterval(progressHandler, 200);
    }
    private function pauseHandler(evt:MouseEvent = null):void { 
      _ns.pause();
      _play_btn.visible = true;
      _pause_btn.visible = false;
      clearInterval(_play_interval);
    }
    private function stop_handler(evt:MouseEvent):void {
      pauseHandler();
      _ns.close();
      _is_playing_bl = false;
      clearInterval(_load_interval);
    }
    private function setPlayheadHandler(evt:MouseEvent):void {
      
    }
    private function netStatusHandler(evt:NetStatusEvent):void {
      switch(evt.info.code) {
        case 'NetConnection.Connect.Success':
          connectStream();
          break;
        
        case 'NetStream.Play.StreamNotFound':
          trace("load movie failed..");
          break;
          
        case 'NetStream.Play.Start':
          trace("load movie ok..");
          _is_playing_bl = true;
          break;
          
        case 'NetStream.Play.Stop':
        
          break;
      }
    }
    private function connectStream():void {
      _ns = new NetStream(_nc);
      _ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
      _ns.bufferTime = 10;
      _ns.client = this;
      _video = new Video(_width_uint, _height_uint);
      
      _video.attachNetStream(_ns);
    }
    private function dispatchResize():void {
      var _evt:Event = new Event(Event.RESIZE);
      dispatchEvent(_evt);
    }
    private function removedHandler(evt:Event):void {
      if (_is_inside_bl) {
        pauseHandler();
      }
    }
    private function progressHandler():void {
      _loaded_bar.width = uint(_width_uint * _ns.bytesLoaded / _ns.bytesTotal);
      if (_ns.bytesLoaded == _ns.bytesTotal) {
        clearInterval(_load_interval);
      }
    }
    private function playingHandler():void {
      _played_bar.width = uint(_width_uint * _ns.time / _movie_length_uint);
    }
    private function movieFitSize():void {
      if (_ori_width_uint / _ori_height_uint > _width_uint / _height_uint) {
        _video.height = _ori_height_uint / _ori_width_uint * _width_uint;
      } else {
        _video.width = _ori_width_uint / _ori_height_uint * _height_uint;
      }
    }
		private function setVolume(evt:MouseEvent):void {
			_sound_bar.width = evt.localX * _sound_bg.scaleX;
			silentHandler();
		}
		private function muteHandler(evt:MouseEvent):void {
			_mute_btn.visible = false;
			_silent_btn.visible = true;
			_volume.volume = 0;
			_ns.soundTransform = _volume;
		}
		private function silentHandler(evt:MouseEvent = null):void { 
			_mute_btn.visible = true;
			_silent_btn.visible = false;
			_volume.volume = _sound_bar.width / _sound_bg.width;
			_ns.soundTransform = _volume;
		}
    
    /***************
     * methods
     * ************/
    public function onMetaData(info:Object):void {
      _movie_length_uint = uint(info.duration);
      if (info.width && info.height) {
        _ori_width_uint = info.width;
        _ori_height_uint = info.height;
        movieFitSize();
      }
    }
    public function onCuePoint(info:Object):void {
      
    }
    public function addVideoInside():void {
      // 把现有按钮往下移
      for (var i:uint = 0; i < numChildren; i++) {
        getChildAt(i).y += _height_uint;
      }
      // 绘制影片背景
      graphics.beginFill(0x000000);
      graphics.drawRect(0, 0, _width_uint, _height_uint);
      graphics.endFill();
      addChild(_video);
      
      _is_inside_bl = true;
      dispatchResize();
    }
    public function addVideoOutside():Video {
      dispatchResize();
      return _video;
    }
    public function playMovie():void {
      playHandler();
    }
  }
}